/**
*   FuzzyEngine - an Simple flex framework 
*   Copyright (C) 2010 Constantin Aldea
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package main.com.sibilla.model
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.core.Application;
	import mx.utils.URLUtil;
	public class Connector extends EventDispatcher
	{
		public static const LOCAL:int = 0;
		public static const PREPROD:int = 1;
		public static const PRODUCTION:int = 2;
		
		public static const GATEWAY:String = "service";
		/**
		 * 
		 * @param mode tipologia connessione 
		 * @param target una target (da sviluppare)
		 * 
		 */		
		
		public function Connector(mode:int=PRODUCTION, custom:String=null, target:IEventDispatcher=null)
		{
			super(target);
			this.intialize_connection(mode, custom)
		}
		
		private var _url_Fuzzy:String;
		
		private function intialize_connection(mode:int=Connector.PRODUCTION, custom:String=null):void{
			switch(mode){
				case Connector.LOCAL:
					this.initialize_local(custom);
				break;
				case Connector.PREPROD:
				break;
					this.initialize_production()
				case Connector.PRODUCTION:
					this.initialize_production();
				break;
			}
		}

		public function get url_Fuzzy():String
		{
			return _url_Fuzzy;
		}
		

		public function set url_Fuzzy(val:String):void
		{
			_url_Fuzzy = val;
		}
		
		protected function initialize_local(custom:String=null):void{
			if(custom != null){
				if(custom.length < 5){
					throw new Error("e possibile che sia un errore di configurazione. La stringa di connessione non corretta");
				}
				url_Fuzzy = custom;
			}else{	
				var servicePoint:String = (mx.core.Application.application.url.split("/") as Array)[3];	
		 		url_Fuzzy = URLUtil.getProtocol(mx.core.Application.application.url)+"://"+URLUtil.getServerNameWithPort(mx.core.Application.application.url)+"/"+servicePoint+"/"+GATEWAY;
			}
			
			trace(url_Fuzzy);
		}
		
		protected function initialize_preproduction():void{
				var servicePoint:String = (mx.core.Application.application.url.split("/") as Array)[3];	
		 		url_Fuzzy = URLUtil.getProtocol(mx.core.Application.application.url)+"://"+URLUtil.getServerNameWithPort(mx.core.Application.application.url)+"/"+servicePoint+"/"+GATEWAY;
		}
		
		protected function initialize_production():void{
				
				var servicePoint:String = (mx.core.Application.application.url.split("/") as Array)[3];	
		 		url_Fuzzy = URLUtil.getProtocol(mx.core.Application.application.url)+"://"+URLUtil.getServerNameWithPort(mx.core.Application.application.url)+"/"+servicePoint+"/"+GATEWAY;
		}
	}
}